package util;//package util;
//
//import java.io.File;
//import java.io.FileReader;
//import java.io.FileWriter;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//import static com.hzs.util.PdfUtil.readRdfFile;
//
///**
// * @author ：mmzs
// * @date ：Created in 2020/7/13 16:48
// * @description：处理pdf文件，抽取引文等信息
// * @modified By：
// * @version: 1$
// */
//public class PdfProccess {
//
//    public void readPdf() {
//        File dictory = new File("G:\\AIPDF");
//        for (File file : dictory.listFiles()) {
//            String s = readRdfFile(file);
//            try {
//                removeHeaderAndFooter(s);
//                FileWriter fileWriter = new FileWriter(new File("G:\\AITXT\\" + file.getName().replace(".pdf", ".txt")));
//                fileWriter.write(s);
//                fileWriter.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
//
//    }
//
//    public void readTxt() {
//        File dictory = new File("G:\\AITXT");
//        //先不管错误字符
//        for (File file : dictory.listFiles()) {
////        File file = new File("G:\\AITXT\\1-s2.0-S0004370214001131-main.txt");
////            System.out.println(file.getName());
//            String txtString = readTxt(file);
//            try {
//                txtString = removeHeaderAndFooter(txtString);
//                if ("new".equals(txtString)) {
//                    System.out.println(file.getName());
//                }
////                txtString = removeFooter(txtString);
//            } catch (Exception e) {
//                e.printStackTrace();
//                System.out.println("异常：" + file.getName());
//            }
//
//        }
//    }
//
//    public static String readTxt(File file) {
//        String result = null;
//        try {
//            FileReader fileReader = new FileReader(file);
//            int ch = 0;
//            StringBuffer stringBuffer = new StringBuffer();
//            while ((ch = fileReader.read()) != -1) {
//                if (((char) ch) != '\r') {
//                    stringBuffer.append((char) ch);
//                }
//            }
//            result = stringBuffer.toString();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    public int index = 0;
//    //0：初始；1：正常；2：没有reference；3：参考文献没序号
//
////    public void convertToXml(File file) {
////
////        String s = readRdfFile(file);
////
////        RxivPaperMapper rxivPaperMapper = SpringUtil.getBean(RxivPaperMapper.class);
////        List<RxivPaper> rxivPaperList = rxivPaperMapper.selectPaperByFilename(file.getName());
////        RxivPaper rxivPaper = rxivPaperList.get(0);
////        String journal = rxivPaper.getJournal();
////        //先识别页眉
////        String header = removeHeaderAndFooter(journal, s);
////        //去掉所有页眉
////        if (header != null) {
////            header = header.replaceAll("\\(", "\\\\(");
////            header = header.replaceAll("\\)", "\\\\)");
////            s = s.replaceAll(header, "");
////        }
////
////        //去掉页脚页码
////        Matcher m2 = Pattern.compile("\\s*1\\s*\r\n").matcher(s);
////        if (m2.lookingAt()) {
////            s = s.replaceFirst("\\s*1\\s*\r\n", "");
////            int start = m2.start();
////            int page = 2;
////            do {
////                m2 = Pattern.compile("\\s*" + page +
////                        "\\s*\r\n").matcher(s.substring(start));
////                if (m2.find()) {
////                    s = s.substring(0, start)
////                            + s.substring(start).replaceFirst("\r\n\\s*" +
////                            page + "\\s*\r\n", "\r\n");
////                    start = start + m2.start() + 2;
////                    page++;
////                } else {
////                    break;
////                }
////            } while (1 == 1);
////        }
////
////        //如果有行号，要去掉
////        s = removeLineNumber(s);
////
////        //存为txt文件
//////        try {
//////            File txtFile = new File("D://rxivTxt//" + rxivPaper.getXmlfile() + ".txt");
//////            FileWriter fileWriter = new FileWriter(txtFile);
//////            fileWriter.write(s);
//////            fileWriter.close();
//////        } catch (Exception e) {
//////
//////        }
////
////
////
////        String pattern = "R(?i)EFERENCE(S?)(:?)(\\sand Notes)?\\s*\\d*\\s*\r\n";
////        Pattern r = Pattern.compile(pattern);
////        Matcher m = r.matcher(s);
////        if (m.find()){
////
////            s = s.substring(m.end());
////            //抽出一条条的reference
////            List<String> referenceList = getReferenceFromLongText(s);
////            if (referenceList.size() != 0) {
////                saveToXmlFile(rxivPaper, referenceList);
////                index = 1;
////            }
////        } else {
////            index = 2;
////        }
////
////        rxivPaper.setOldPaper(index);
////        rxivPaperMapper.updateByPrimaryKeySelective(rxivPaper);
////
////    }
//
////    public void saveToXmlFile(RxivPaper rxivPaper, List<String> referenceList) {
////
////        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
////        DocumentBuilder builder = null;
////        try {
////            builder = documentBuilderFactory.newDocumentBuilder();
////            Document document = builder.newDocument();
////            //根
////            org.w3c.dom.Element root = document.createElement("root");
////            //题目
////            org.w3c.dom.Element titleElement = document.createElement("title");
////            titleElement.setTextContent(rxivPaper.getTitle());
////            root.appendChild(titleElement);
////
////            Element referencesElement = document.createElement("references");
////            for (String ref : referenceList) {
////                Element referenceElement = document.createElement("reference");
////                referenceElement.setTextContent(ref);
////                referencesElement.appendChild(referenceElement);
////            }
////            root.appendChild(referencesElement);
////            document.appendChild(root);
////
////            //创建转换工厂，然后将创建的document转换输出到文件中或控制台
////            TransformerFactory transformerFactory = TransformerFactory.newInstance();
////            Transformer transformer = transformerFactory.newTransformer();
////            transformer.transform(new DOMSource(document), new StreamResult(new File("D:\\pdfToXml\\" +
////                    rxivPaper.getXmlfile() + ".xml")));
////
////            //将document中的信息转换为字符串输出到控制台中
////            StringWriter stringWriter = new StringWriter();
////            transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
////
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
////    }
//
//    public String removeHeaderAndFooter(String input) throws Exception {
//        String s = removeHeaderAndFooterOnNewVersionPdf(input);
//        if ("fail".equals(s)) {
//            s = removeHeaderAndFooterOnOldVersionPdf(input);
//        }
//        return s;
//
////        if (input.contains("Contents lists available at")) {
////            return removeHeaderAndFooterOnNewVersionPdf(input);
////        } else {
////            return removeHeaderAndFooterOnOldVersionPdf(input);
////        }
//    }
//
//    public String removeReference(String inputText) {
//        String referencePattern = "\nReferences\n";
//        Matcher m = Pattern.compile(referencePattern).matcher(inputText);
//        if (m.find()) {
//            inputText = inputText.substring(0, m.start());
//        } else {
//            System.out.println("找不到参考文献");
//        }
//        return inputText;
//    }
//
//    public String removeHeaderAndFooterOnNewVersionPdf(String input) {
//        String headerPattern = "/ Artificial Intelligence \\d+ \\(\\d+\\) \\d+\\–\\d+";
//        Matcher m = Pattern.compile(headerPattern).matcher(input);
//        if (m.find()) {
//            int start = m.start();
//            int end = m.end();
//            String twoChar = null;
//            do {
//                start--;
//                twoChar = input.substring(start - 1, start);
//            } while (!twoChar.equals("\n"));
//            String header = input.substring(start, end);
//
////            这里是第一次出现页眉的位置,开始往上找页脚
//            int footerEnd = start;
//            int footerLineStart = start;
//            int footerLineEnd = -1;
//            String footerLine = null;
//            do {
//                footerLineEnd = footerLineStart;
//                footerLineStart = readLineReverse(input, footerLineEnd);
//                footerLine = input.substring(footerLineStart, footerLineEnd);
//            } while (isFooterLine(footerLine));
////            页脚找到了，去掉页脚
//            String footerText = input.substring(footerLineEnd, footerEnd);
//            input = input.replace(footerText, "");
//
////            开始找页眉
//            String[] headerSplit = header.split(" ");
//            String[] pages = headerSplit[headerSplit.length - 1].split("–");
//            int startPage = Integer.parseInt(pages[0]);
//            int endPage = Integer.parseInt(pages[1]);
//            if (startPage % 2 == 1 && header.startsWith(Integer.toString(startPage + 1))) {
//                header = header.substring(pages[0].length() + 1);
//            }
////            奇数在右，偶数在左
//            for (int i = startPage + 1; i <= endPage; i++) {
//                String textToBeReplaced = null;
//                if (i % 2 == 1) {
//                    textToBeReplaced = header + " " + i;
//                } else {
//                    textToBeReplaced = "" + i + " " + header;
//                }
//                if (input.contains(textToBeReplaced)) {
////                    有些没有页眉
//                    input = input.replace(textToBeReplaced, "");
////                    页眉被去掉了
//                }
//            }
//        } else {
//            return "fail";
//        }
//        return input;
//    }
//
//    public String removeHeaderAndFooterOnOldVersionPdf(String input) {
//        String headerPattern = "/Ar\\S+ I";
//        StringBuilder result = new StringBuilder(input);
//        Matcher m = Pattern.compile(headerPattern).matcher(result);
//        int i = 0;
//        while (m.find()) {
//            int[] ints = readLineCurrent(result, m.start());
//            if (i == 0) {
//                i = 1;
////                这里是第一次出现页眉的位置,开始往上找页脚
//                int footerEnd = ints[0];
//                int footerLineStart = ints[0];
//                int footerLineEnd = -1;
//                String footerLine = null;
//                do {
//                    footerLineEnd = footerLineStart;
//                    footerLineStart = readLineReverse(result, footerLineEnd);
//                    footerLine = result.substring(footerLineStart, footerLineEnd);
//                } while (isFooterLine(footerLine));
////            页脚找到了，去掉页脚
//                result.replace(footerLineEnd, footerEnd, "");
//            } else {
//                result.replace(ints[0], ints[1], "");
//            }
//            m.reset();
//        }
//        return result.toString();
//    }
//
//    public int readLineAlong(String inputText, int startIndex) {
//        int end = startIndex;
//        if (inputText.charAt(end + 1) == '\n') {
//            end++;
//        }
//        do {
//            end++;
//        } while (inputText.charAt(end) != '\n');
//        return end + 1;
//    }
//
//    public int[] readLineCurrent(CharSequence inputText, int index) {
//        int start = index;
//        do {
//            start--;
//        } while (inputText.charAt(start) != '\n');
//        int end = index;
//        do {
//            end++;
//        } while (inputText.charAt(end) != '\n');
//        int[] result = new int[2];
//        result[0] = start + 1;
//        result[1] = end + 1;
//        return result;
//    }
//
//    public List<String> getReferenceFromLongText(String input) {
//        //前面已经去掉页眉页脚
//        List<String> result = new ArrayList<>();
//        Matcher m = null;
//        //哪一种类型的序号?
//        int labelType = -1;
//        int referenceType = -1;
//        int label = 2;
//        if (input.startsWith("1 ")) {
//            //数字加空格
//            labelType = 1;
//        } else if (input.startsWith("1.")) {
//            labelType = 2;
//        } else if (input.startsWith("1")) {
//            labelType = 3;
//        } else if (input.startsWith("[1]")) {
//            labelType = 4;
//        } else {
//            index = 3;
//            return result;
//        }
//
//        //没有序号怎么办？没有序号就不用做了
//
//        String leftPattern = null;
//        String rightPattern = null;
//        switch (labelType) {
//            case 1:
//                leftPattern = "\r\n *";
//                rightPattern = " *";
//                break;
//            case 2:
//                leftPattern = "\r\n *";
//                rightPattern = "\\. *";
//                break;
//            case 3:
//                leftPattern = "\r\n *";
//                rightPattern = "";
//                break;
//            case 4:
//                leftPattern = "\r\n *\\[";
//                rightPattern = "\\] *";
//                break;
//            default:
//                ;
//        }
//
//        m = Pattern.compile(leftPattern + label + rightPattern).matcher(input);
//        m.find();
//
//        do {
//            int referenceEnd = m.start();
//            String reference = input.substring(0, referenceEnd);
//
//            result.add(reference);
//
//            label++;
//            input = input.substring(referenceEnd + 2);
//            m = Pattern.compile(leftPattern + label + rightPattern).matcher(input);
//        } while (m.find());
//
//        //补上最后一条reference，最后一条到哪算完？连续两个/r/n算完
//        boolean foundLastReference = false;
//        String lastReference = "";
//        do {
//            m = Pattern.compile("\r\n").matcher(input);
//            m.find();
//            lastReference = lastReference.concat(input.substring(0, m.start()));
//            input = input.substring(m.end());
//            m = Pattern.compile("\\s*\r\n").matcher(input);
//            if (m.lookingAt()) {
//                foundLastReference = true;
//            }
//            if (input.trim().equals("")) {
//                foundLastReference = true;
//            }
//        } while (!foundLastReference);
//        result.add(lastReference);
//
//        return result;
//    }
//
//    /**
//     * create by: mmzs
//     * description: 倒序读一行
//     * create time: 2020/7/21 16:27
//     *
//     * @return
//     * @Param: null
//     */
//    public int readLineReverse(CharSequence input, int endIndex) {
//        int start = endIndex;
//        if (input.charAt(start - 1) == '\n') {
//            start--;
//        }
//        do {
//            start--;
//        } while (input.charAt(start) != '\n');
//        return start + 1;
//    }
//
//    public boolean isFooterLine(String input) {
//        String[] arr = new String[]{
//                "http://","https://","All rights reserved","Corresponding author",
//                "doi:","URL:","Tel.","PII","pii","Email:","SSDI","E-mail","phone"
//        };
//        for (String s : arr) {
//            if (input.contains(s)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    public static void main(String[] args) throws Exception {
//        new PdfProccess().readTxt();
//
//    }
//
//}
